package com.umeng.comm.ui.widgets;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by moyuwan on 16/5/25.
 */
public class HomeImageView extends ImageView {

    private Context mContext;

    private int mIntrinsicWidth;
    private int mIntrinsicHeight;

    private Matrix mMatrix;

    public HomeImageView(Context context, AttributeSet attr) {
        super(context, attr);
        this.mContext = context;
        initialize();
    }

    public HomeImageView(Context context) {
        super(context);
        this.mContext = context;
        initialize();
    }

    private void initialize() {
        this.setScaleType(ScaleType.MATRIX);
        this.mMatrix = new Matrix();
        Drawable d = getDrawable();
        if (d != null) {
            mIntrinsicWidth = d.getIntrinsicWidth();
            mIntrinsicHeight = d.getIntrinsicHeight();
        }
//        LogUtil.d("### 图片宽度 : " + mIntrinsicWidth + ", height : " + mIntrinsicHeight);
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        this.initialize();
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        this.initialize();
    }

    public void updateScale(float mScale) {
        if (mScale > 0) {
            mMatrix.reset();
            mMatrix.postScale(mScale, mScale);
            setImageMatrix(mMatrix);
        }
    }


}
